package com.ly.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ly.mapper.*;
import com.ly.pojo.dto.CourseDTO;
import com.ly.pojo.dto.LessonDTO;
import com.ly.pojo.dto.SectionDTO;
import com.ly.pojo.entity.*;
import com.ly.service.CourseService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
public class CourseServiceImpl implements CourseService {
    @Autowired
    @Qualifier("diyRedisTemplate")
    private RedisTemplate<String, Object> redisTemplate;
    @Autowired
    private TeacheMapper teacheMapper;
    @Autowired
    private CourseMapper courseMapper;
    @Autowired
    private CourseLessonMapper courseLessonMapper;
    @Autowired
    private CourseSectionMapper courseSectionMapper;
    @Autowired
    private CourseMediaMapper courseMediaMapper;

    //获取所有课程及关联的前两节课时信息
    @Override
    public List<CourseDTO> findAllCourse() {
        List<CourseDTO> allcourse = (List<CourseDTO>) redisTemplate.opsForValue().get("allcourse");//从redis中获取课程信息
        //用redis处理缓存穿透的情况 双层检测锁
        if (null == allcourse){//若为空则从数据库中进行搜索
            List<Course> courseList = getCourseList();
            List<CourseDTO> dtoList = new ArrayList<>();
            synchronized (this){
                allcourse = (List<CourseDTO>) redisTemplate.opsForValue().get("allcourse");
                if (null == allcourse){
                    for (Course course : courseList) {
                        CourseDTO dto = new CourseDTO();
                        BeanUtils.copyProperties(course, dto);
                        getTeacher(dto);
                        getCourseLessonList(dto);
                        //getSectionDto(dto);
                        dtoList.add(dto);
                    }
                    redisTemplate.opsForValue().set("allcourse",dtoList,30, TimeUnit.MINUTES);
                }
            }
        }
        return allcourse;
    }
    //通过id获取课程信息
    @Override
    public CourseDTO getCourseById(Integer courseId) {
        Course course = courseMapper.selectOne(new QueryWrapper<Course>().eq("id", courseId));
        //复制数据
        CourseDTO dto = new CourseDTO();
        BeanUtils.copyProperties(course, dto);
        //获取teacher
        getTeacher(dto);
        //获取章节及章节下的课时信息
        getSectionDto(dto);
        return dto;
    }

    //获取所有course
    private List<Course> getCourseList() {
        QueryWrapper<Course> wrapper = new QueryWrapper<>();
        wrapper
                .eq("status", 1)
                .eq("is_del", 0)
                .orderByDesc("sort_num");
        List<Course> courseList = courseMapper.selectList(wrapper);
        return courseList;
    }
    //获取teacher
    private void getTeacher(CourseDTO dto) {
        QueryWrapper<Teacher> wrapper = new QueryWrapper<>();
        wrapper
                .eq("course_id", dto.getId())
                .eq("is_del", 0);
        dto.setTeacher(teacheMapper.selectOne(wrapper));
    }
    //获取前两节课时
    private void getCourseLessonList(CourseDTO dto) {
        QueryWrapper<CourseLesson> wrapper = new QueryWrapper<>();
        wrapper
                .eq("course_id", dto.getId())
                .eq("status", 2)
                .eq("is_del", 0)
                .orderByDesc("section_id", "order_num")
                .last("limit 0 , 2");
        dto.setLessonList(courseLessonMapper.selectList(wrapper));
    }
    //获取章节及对应的课时
    private void getSectionDto(CourseDTO dto) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("course_id", dto.getId());
        wrapper.eq("is_del", 0);
        wrapper.eq("status", 2);
        wrapper.orderByDesc("order_num");
        //获取章节基本信息
        List<CourseSection> sections = courseSectionMapper.selectList(wrapper);//查询出2个章节
        //创建sectiondto 集合
        List<SectionDTO> sectionDTOS = new ArrayList<>();
        //关联章节集合
        for (CourseSection section : sections) {
            SectionDTO sectionDTO = new SectionDTO();
            //复制数据
            BeanUtils.copyProperties(section, sectionDTO);
            wrapper.clear();
            //获取章节下所有的课时基本类信息
            wrapper.eq("section_id",sectionDTO.getId());
            wrapper.eq("is_del",0);
            wrapper.eq("status",2);
            wrapper.orderByDesc("order_num");
            List<CourseLesson> lessonList = courseLessonMapper.selectList(wrapper);
            List<LessonDTO> dtoList = new ArrayList<>();

            for (CourseLesson lesson : lessonList) {
                LessonDTO lessonDTO = new LessonDTO();
                //复制数据
                BeanUtils.copyProperties(lesson,lessonDTO);
                getMedia(lessonDTO);
                dtoList.add(lessonDTO);
            }
            sectionDTO.setLessonDTOList(dtoList);
            sectionDTOS.add(sectionDTO);
        }
        dto.setSectionList(sectionDTOS);
    }
    //获取课时media
    private void getMedia(LessonDTO dto){
        QueryWrapper<CourseMedia> wrapper = new QueryWrapper<>();
        wrapper
                .eq("lesson_id",dto.getId())
                .eq("is_del",0);
        dto.setCourseMedia(courseMediaMapper.selectOne(wrapper));
    }
}
